#
# Package to generate the LaTeX output
#
# 2009, Mike Widner <mikewidner@gmail.com>
#
############

package Fangcheng::LaTeX;

use 5.006001;
use strict;
use warnings;
use vars qw(@ISA);
use Carp;

require Fangcheng;

@ISA = qw( Fangcheng );
our $VERSION = '0.01';

# creates string for LaTeX
sub matrix {	  
	my ($self, $matrix) = @_;
	my $string; # = '\\dpi{100}\\def\\zN{$N}\\def\\zL{$L}\\fbox{\\xymatrix \@H= 5mm {$xymatrix[$s]}}';
				# $N = rows(), $L = $N - 1
	
	for ( my $row = 1; $row <= $matrix->rows(); $row++ ) {
		for ( my $col = 1; $col <= $matrix->rows(); $col++ ) {
			my $val = $matrix->get( $row, $col );
			if ( $val >= 0 ) {
				$string .= qq|\\pn|;			
			}
			$string .= qq| $val &|;			
		}
		my $val = $matrix->get( $row, $matrix->columns() );
		if ( $val >= 0 ) {
			$string .= qq|\\pn|;
		}
		$string .= qq| $val \\\\ |;
	}

	return( $self->wrapMatrix($string) );
}

# wraps matrix string in definitions
sub wrapMatrix {
	my ($self, $string) = @_;
	$string = '\\usepackage{euler}\\begin{equation*}\\begin{bmatrix}' . $string . '\\end{bmatrix}\\end{equation*}';
	return( $string );
}

sub makeBoxes {
	my ($self, $matrix) = @_;
	my $grid = Fangcheng::Matrix->new( $matrix->rows, '' );
	foreach my $row ( reverse( 1 .. $matrix->rows() ) ) {
		foreach my $col ( 1 .. $matrix->columns() ) {
			$grid->set( $row, $col, 'm' );
		}
	}
	return( $grid );
}

sub toString {
	my ($self, $matrix) = @_;
	my $str = '';
	
	foreach my $row ( 1 .. $matrix->rows() ) {
		foreach my $col ( 1 .. $matrix->columns() ) {
			$str .= $matrix->get( $row, $col );
		}
	}
	
	return( $self->wrapString($str) );	
}
1;